package com.supermap.business.monitor;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.supermap.dataservice.util.HttpRequestUtil;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.agrinfo.AgrinfoService;
import com.supermap.dubbo.api.expertinfo.ExpertInfoService;
import com.supermap.dubbo.api.farmland.FarmlandService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "monitor")
public class MonitorController {

    @Reference
    private FarmlandService farmlandService;

    @Reference
    private ExpertInfoService expertInfoService;

    @Reference
    private AgrinfoService agrinfoService;

    /**
     * 获取某地区三类服务产品个数
     *
     * @param request HTTPRequest
     * @return
     */
    @RequestMapping(value = "getProductCountByArea", method = RequestMethod.GET)
    public JSONUtil getProductCountByArea(HttpServletRequest request) {
        String url = HttpRequestUtil.SMS_URL + HttpRequestUtil.PRODUCT_COUNT_URL;
        JSONObject param = new JSONObject();
        param.put("areaCode", request.getParameter("areaCode"));
        JSONUtil result = new JSONUtil();
        result.setIsSuccess(true);
        result.setCode(100);
        JSONObject data = HttpRequestUtil.httpGet(url, param);
        if (data != null) {
            result.setCode(200);
            result.setData(data);
        }
        return result;
    }

    /**
     * 获取某地区下每区域的注册农田数量
     *
     * @param request HTTPRequest
     * @return
     */
    @RequestMapping(value = "getFarmlandCountByArea", method = RequestMethod.GET)
    public JSONUtil getFarmlandCountByArea(HttpServletRequest request) {
        String[] areaCodes = null;
        if (request.getParameter("areaCode") != null) {
            areaCodes = request.getParameter("areaCode").split(",");
        }
        return farmlandService.getFarmlandCountByArea(areaCodes);
    }

    /**
     * 获取某地区前n个活跃的专家列表，并返回活跃度
     *
     * @param request HTTPRequest
     * @return
     */
    @RequestMapping(value = "getPositiveExpertByArea", method = RequestMethod.GET)
    public JSONUtil getPositiveExpertByArea(HttpServletRequest request) {
        String count = request.getParameter("count");
        String[] areaCodes = null;
        if (request.getParameter("areaCode") != null) {
            areaCodes = request.getParameter("areaCode").split(",");
        }
        return expertInfoService.getPositiveExpertByArea(count, areaCodes);
    }

    /**
     * 获取程序执行状态
     *
     * @param request HTTPRequest
     * @return
     */
    @RequestMapping(value = "getProgramStatusByArea", method = RequestMethod.GET)
    public JSONUtil getProgramStatusByArea(HttpServletRequest request) {
        return null;
    }

    /**
     * 获取各分类的服务对象数量
     *
     * @param request HTTPRequest
     * @return
     */
    @RequestMapping(value = "getServiceTargetDetail", method = RequestMethod.GET)
    public JSONUtil getServiceTargetDetail(HttpServletRequest request) {
        return null;
    }

    /**
     * 获取某地区各分类在当年每个月农情上传的数量
     *
     * @param request HTTPRequest
     * @return
     */
    @RequestMapping(value = "getAgriMsgCountByArea", method = RequestMethod.GET)
    public JSONUtil getAgriMsgCountByArea(HttpServletRequest request) {
        String[] areaCodes = null;
        if (request.getParameter("areaCode") != null) {
            areaCodes = request.getParameter("areaCode").split(",");
        }
        String year = request.getParameter("year");
        return agrinfoService.getAgriMsgCountByArea(areaCodes, year);
    }

    /**
     * 获取某地区实景监控点列表
     *
     * @param request HTTPRequest
     * @return
     */
    @RequestMapping(value = "getMonitorListByArea", method = RequestMethod.GET)
    public JSONUtil getMonitorListByArea(HttpServletRequest request) {
        return null;
    }

    /**
     * 点击某实景监控点的时候返回视频流
     *
     * @param request HTTPRequest
     * @return
     */
    @RequestMapping(value = "getMonitorDetailsById", method = RequestMethod.GET)
    public JSONUtil getMonitorDetailsById(HttpServletRequest request) {
        return null;
    }

    /**
     * 获取某地区每个地方当年发布总数,以及三类产品具体个数
     *
     * @param request HTTPRequest
     * @return
     */
    @RequestMapping(value = "getProductDetialsByArea", method = RequestMethod.GET)
    public JSONUtil getProductDetialsByArea(HttpServletRequest request) {
        String url = HttpRequestUtil.SMS_URL + HttpRequestUtil.PRODUCT_DETIAL_COUNT_URL;
        JSONObject param = new JSONObject();
        param.put("areaCode", request.getParameter("areaCode"));
        JSONUtil result = new JSONUtil();
        result.setIsSuccess(true);
        result.setCode(100);
        JSONObject data = HttpRequestUtil.httpGet(url, param);
        if (data != null) {
            result.setCode(200);
            result.setData(data);
        }
        return result;
    }

    /**
     * 获取某地区当年每月发布总数
     *
     * @param request HTTPRequest
     * @return
     */
    @RequestMapping(value = "getProductPublishMonthlyByArea", method = RequestMethod.GET)
    public JSONUtil getProductPublishMonthlyByArea(HttpServletRequest request) {
        String url = HttpRequestUtil.SMS_URL + HttpRequestUtil.PRODUCT_PUBLISH_COUNT_URL;
        JSONObject param = new JSONObject();
        param.put("areaCode", request.getParameter("areaCode"));
        JSONUtil result = new JSONUtil();
        result.setIsSuccess(true);
        result.setCode(100);
        JSONObject data = HttpRequestUtil.httpGet(url, param);
        if (data != null) {
            result.setCode(200);
            result.setData(data);
        }
        return result;
    }

}
